package DMSXL._2024.BinaryTrees;


import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class preorderTraversal {
    public static void main(String[] args) {
        int[] nodes = {1,-1,2,3};
        TreeNode root = new TreeNode(nodes[0]);
        root.left = null;
        root.right = new TreeNode(2);
        root.right.left = new TreeNode(3);

        System.out.println(new preorderTraversal().preorderTraversal(root));

    }
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        Stack<TreeNode> stack = new Stack();
        if(root == null){
            return result;
        }
        stack.push(root);
        while (!stack.isEmpty()){
            TreeNode cur = stack.pop();
            result.add(cur.val);
            if(cur.right != null){
                stack.push(cur.right);
            }
            if(cur.left != null){
                stack.push(cur.left);
            }
        }
        return result;
    }
}
